package com.example.itshome.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Lifecycle;
import com.bumptech.glide.Glide;
import com.example.itshome.R;
import com.example.itshome.activity.*;
import com.example.itshome.manager.UserInfoManager;
import com.example.itshome.onclick.OnClickTurnTo;

/**
 * A simple {@link Fragment} subclass.
 * create an instance of this fragment.
 */
public class MyPageFragment extends Fragment implements UserInfoManager.OnUpdateUserRunner {

    private ImageView setImageView;
    private ImageView headerImageView;
    private TextView nicknameTextView;
    private TextView introductionTextView;
    private TextView postCountTextView;
    private TextView collectionCountTextView;
    private TextView integralCountTextView;
    private View signInActivityLayout;
    private View signUpActivityLayout;
    private View joinedOrganizationLayout;
    private View myFavoriteLayout;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view=inflater.inflate(R.layout.fragment_my_page, container, false);
        initFind(view);
        initButton();
        UserInfoManager.getInstance().addRunners(this);
        return view;
    }

    void initFind(View v){
        setImageView=v.findViewById(R.id.imv_my_page_set);
        signInActivityLayout=v.findViewById(R.id.layout_my_page_signed_in_activity);
        signUpActivityLayout=v.findViewById(R.id.layout_my_page_signed_up_activity);
        joinedOrganizationLayout=v.findViewById(R.id.layout_my_page_joined_organization);
        myFavoriteLayout=v.findViewById(R.id.layout_my_page_my_favorite);
        headerImageView=v.findViewById(R.id.imv_my_page_header);
        nicknameTextView=v.findViewById(R.id.txv_my_page_nickname);
        introductionTextView=v.findViewById(R.id.txv_my_page_introduction);
        postCountTextView=v.findViewById(R.id.txv_my_page_post_count);
        collectionCountTextView=v.findViewById(R.id.txv_my_page_collection_count);
        integralCountTextView=v.findViewById(R.id.txv_my_page_integral_count);
    }

    void initView(){
        if (getContext()==null){
            return;
        }
        Glide.with(this).load(UserInfoManager.getInstance().getUser().getHeaderSrc())
                        .circleCrop()
                        .placeholder(R.drawable.my_page_header_example).into(headerImageView);
        nicknameTextView.setText(UserInfoManager.getInstance().getUser().getNickname());
        introductionTextView.setText(UserInfoManager.getInstance().getUser().getIntroduction());
        postCountTextView.setText(String.valueOf(UserInfoManager.getInstance().getUser().getPostCount()));
        collectionCountTextView.setText(String.valueOf(UserInfoManager.getInstance().getUser().getCollectionCount()));
        integralCountTextView.setText(String.valueOf(UserInfoManager.getInstance().getUser().getIntegral()));
    }

    void initButton(){
        setImageView.setOnClickListener(new OnClickTurnTo(getActivity(),SetPageActivity.class));
        signInActivityLayout.setOnClickListener(new OnClickTurnTo(getActivity(), SignedInActivityPageActivity.class));
        signUpActivityLayout.setOnClickListener(new OnClickTurnTo(getActivity(), SignedUpActivityPageActivity.class));
        joinedOrganizationLayout.setOnClickListener(new OnClickTurnTo(getActivity(), JoinedOrganizationPageActivity.class));
        myFavoriteLayout.setOnClickListener(new OnClickTurnTo(getActivity(), MyFavoritePageActivity.class));
    }

    @Override
    public void onInfoUpdate() {
        initView();
    }
}